using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Linq;
using System.Web;
using System.Configuration;
using Lincolnwood.Foundation.Common.Data.NHibernate;
using System.Linq;
using Lincolnwood.Foundation.Common.Data;

namespace Lincolnwood.Foundation.SermonLibrary.Data.NHibernate
{
    public class NHibernateSermonRepository : NHibernateRepositoryBase<Sermon, Guid>,  ISermonRepository
    {        
        public NHibernateSermonRepository(IUnitOfWork unitOfWork)
            : base((NHibernateUnitOfWork)unitOfWork)
        { }


        public IEnumerable<Sermon> GetSermonsPaged(int pageIndex, int itemsPerPage, PublishedStatus status)
        {
            int startRow = pageIndex > 0 ? (pageIndex - 1) * itemsPerPage : 0;

            IQuery query = null;

            switch (status)
            {
                case PublishedStatus.Any:
                    query = UnitOfWork.Session.CreateQuery("FROM Sermon s ORDER BY s.RecordedDate DESC");
                    break;
                case PublishedStatus.Published:
                    query = UnitOfWork.Session.CreateQuery("FROM Sermon s WHERE s.IsPublished = true ORDER BY s.RecordedDate DESC");
                    break;
                case PublishedStatus.Unpublished:
                    query = UnitOfWork.Session.CreateQuery("FROM Sermon s WHERE s.IsPublished = false ORDER BY s.RecordedDate DESC");
                    break;
                default:
                    break;
            }
            query.SetFirstResult(startRow);
            query.SetMaxResults(itemsPerPage);

            IList<Sermon> sermons = query.List<Sermon>();
            return sermons;
        }

        public IEnumerable<Sermon> GetSermonsInSeries(Guid sermonSeriesId)
        {
            var output = from s in UnitOfWork.Session.Query<Sermon>() where s.Series.Id == sermonSeriesId select s;

            IList<Sermon> sermons = output.ToList<Sermon>();

            return sermons;
        }

        public override void Merge(Sermon entity)
        {
            throw new NotImplementedException();
        }
    }
}
